/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制计时器的窗体
* CreateTime：2020-07-07 17:25:03
* Version：1.0
* Modify Recorder：
*************************************************/

using System;
using System.Collections.Generic;
using TinyToolKit;
using TinyToolKit.Timer;
using UnityEditor;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class DrawWindowTimer : DrawWindowBase
    {
        private TTimerManager _timerManager;
        private bool _isFoldoutTimerTask = true;
        private bool _isFoldoutFrameTimerTask = true;

        protected override void DrawInternalMainGUI()
        {
            TinyToolWindowUtil.DrawDefaultEditorGUI();

            if (EditorApplication.isPlaying == false || TinyTool.Instance.HasManager<TTimerManager>() == false) return;

            if (_timerManager == null) _timerManager = TinyTool.Instance.GetManager<TTimerManager>();

            List<TimerTask> timerTasks = _timerManager.TaskTimerList;
            if (timerTasks != null && timerTasks.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                _isFoldoutTimerTask = EditorGUILayout.Foldout(_isFoldoutTimerTask, "定时任务");
                if (_isFoldoutTimerTask)
                {
                    TinyToolWindowUtil.DrawTableHead("ID", "状态");
                    for (int i = 0; i < timerTasks.Count; i++)
                    {
                        string loopTime = timerTasks[i].repeatCount == 0 ? "Loop" : timerTasks[i].repeatCount.ToString();
                        string info = string.Format("运行状态：{0}，持续时间：{1}s，重复次数：{2}，忽视TimeScale：{3}", Enum.GetName(typeof(TaskState), timerTasks[i].taskState), timerTasks[i].duration, loopTime, timerTasks[i].ignoreTimeScale);
                        EditorGUILayout.LabelField(timerTasks[i].taskID.ToString(), info);
                    }
                    TinyToolWindowUtil.DrawTableTail("总计：", timerTasks.Count.ToString());

                }
                EditorGUILayout.EndVertical();
            }

            List<TimerTaskByFrame> timerTasksByFrame = _timerManager.TaskFrameList;
            if (timerTasksByFrame != null && timerTasksByFrame.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                _isFoldoutFrameTimerTask = EditorGUILayout.Foldout(_isFoldoutFrameTimerTask, "帧定时任务");
                if (_isFoldoutFrameTimerTask)
                {
                    TinyToolWindowUtil.DrawTableHead("ID", "状态");
                    for (int i = 0; i < timerTasksByFrame.Count; i++)
                    {
                        string loopTime = timerTasksByFrame[i].repeatCount == 0 ? "Loop" : timerTasksByFrame[i].repeatCount.ToString();
                        string info = string.Format("运行状态：{0}，持续帧数：{1}，重复次数：{2}", Enum.GetName(typeof(TaskState), timerTasksByFrame[i].taskState), timerTasksByFrame[i].frameCount, loopTime);
                        EditorGUILayout.LabelField(timerTasksByFrame[i].taskID.ToString(), info);
                    }
                    TinyToolWindowUtil.DrawTableTail("总计：", timerTasksByFrame.Count.ToString());

                }
                EditorGUILayout.EndVertical();
            }
        }
    }
}